<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book 6节课入门Flask框架web开发 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>定义模型&amp;创建表&amp;数据添加 | 6节课入门Flask框架web开发</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html" />
    
    
    <link rel="prev" href="../../chapter3/zong-he-an-4f8b-tu-shu-guan-li.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="3.3.1"
        data-chapter-title="定义模型&amp;创建表&amp;数据添加"
        data-filepath="chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.md"
        data-basepath="../.."
        data-revision="Sun Jan 28 2018 01:59:46 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Flask快速入门课程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1.html">
            
                
                    <a href="../../chapter1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/flaskjian-jie.html">
            
                
                    <a href="../../chapter1/flaskjian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        为什么要使用WEB框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/flaskyu-django-dui-bi.html">
            
                
                    <a href="../../chapter1/flaskyu-django-dui-bi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter1/flask-an-zhuang-huan-jing.html">
            
                
                    <a href="../../chapter1/flask-an-zhuang-huan-jing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        Flask 安装环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter1/requirements-wen-jian.html">
            
                
                    <a href="../../chapter1/requirements-wen-jian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        requirements 文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter1/hello-worldcheng-xu.html">
            
                
                    <a href="../../chapter1/hello-worldcheng-xu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        Hello World程序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
            
                
                    <a href="../../chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        路由定义的基本方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2.html">
            
                
                    <a href="../../chapter2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter2/jinja2mo-ban-yin-qing.html">
            
                
                    <a href="../../chapter2/jinja2mo-ban-yin-qing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Jinja2模板引擎
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/guo-lv-qi.html">
            
                
                    <a href="../../chapter2/guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="chapter2/hong.html">
            
                
                    <a href="../../chapter2/hong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Flask-WTF表单
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3.html">
            
                
                    <a href="../../chapter3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="chapter3/shu-ju-ku-de-she-zhi.html">
            
                
                    <a href="../../chapter3/shu-ju-ku-de-she-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Flask中使用数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
            
                
                    <a href="../../chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        数据库基本操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
            
                
                    <a href="../../chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        综合案例-图书管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="3.3.1" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
            
                
                    <a href="../../chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        定义模型&amp;创建表&amp;数据添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
            
                
                    <a href="../../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        数据显示&amp;表单添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
            
                
                    <a href="../../chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        添加数据的表单验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
            
                
                    <a href="../../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.4.</b>
                        
                        删除数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >6节课入门Flask框架web开发</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5B9A;&#x4E49;&#x6A21;&#x578B;">&#x5B9A;&#x4E49;&#x6A21;&#x578B;</h1>
<p>&#x6A21;&#x578B;&#x8868;&#x793A;&#x7A0B;&#x5E8F;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x5B9E;&#x4F53;&#xFF0C;&#x5728;Flask-SQLAlchemy&#x4E2D;&#xFF0C;&#x6A21;&#x578B;&#x4E00;&#x822C;&#x662F;Python&#x7C7B;&#xFF0C;&#x7EE7;&#x627F;&#x81EA;db.Model&#xFF0C;db&#x662F;SQLAlchemy&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x4EE3;&#x8868;&#x7A0B;&#x5E8F;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x5E93;&#x3002;</p>
<p>&#x7C7B;&#x4E2D;&#x7684;&#x5C5E;&#x6027;&#x5BF9;&#x5E94;&#x6570;&#x636E;&#x5E93;&#x8868;&#x4E2D;&#x7684;&#x5217;&#x3002;id&#x4E3A;&#x4E3B;&#x952E;&#xFF0C;&#x662F;&#x7531;Flask-SQLAlchemy&#x7BA1;&#x7406;&#x3002;db.Column&#x7C7B;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x6570;&#x636E;&#x5E93;&#x5217;&#x548C;&#x6A21;&#x578B;&#x5C5E;&#x6027;&#x7C7B;&#x578B;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#xFF1A;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5728;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x7684;&#x65F6;&#x5019;&#x6307;&#x5B9A;&#x7F16;&#x7801;&#x7684;&#x8BDD;&#xFF0C;&#x5411;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x63D2;&#x5165;&#x4E2D;&#x6587;&#x540E;&#xFF0C;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x90A3;&#x4E48;&#x9700;&#x8981;&#x4FEE;&#x6539;&#x6570;&#x636E;&#x5E93;&#x7684;&#x7F16;&#x7801;&#x96C6;:</p>
</blockquote>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">alter</span> <span class="hljs-keyword">database</span> &#x6570;&#x636E;&#x5E93;&#x540D; <span class="hljs-built_in">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8
</span></code></pre>
<p>&#x5982;&#x4E0B;&#x793A;&#x4F8B;&#xFF1A;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x6A21;&#x578B;&#x7C7B;&#xFF0C;&#x4F5C;&#x8005;&#x548C;&#x4E66;&#x540D;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># -*- coding:utf-8 -*-</span>

<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask
<span class="hljs-keyword">from</span> flask_sqlalchemy <span class="hljs-keyword">import</span> SQLAlchemy

app = Flask(__name__)

app.config[<span class="hljs-string">&apos;SQLALCHEMY_DATABASE_URI&apos;</span>] = <span class="hljs-string">&apos;mysql://root:mysql@127.0.0.1:3306/test2&apos;</span>
app.config[<span class="hljs-string">&apos;SQLALCHEMY_TRACK_MODIFICATIONS&apos;</span>] = <span class="hljs-keyword">False</span>

db = SQLAlchemy(app)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Author</span><span class="hljs-params">(db.Model)</span>:</span>
    __tablename__ = <span class="hljs-string">&apos;authors&apos;</span>
    id = db.Column(db.Integer, primary_key=<span class="hljs-keyword">True</span>)
    name = db.Column(db.String(<span class="hljs-number">64</span>), unique=<span class="hljs-keyword">True</span>)
    books = db.relationship(<span class="hljs-string">&apos;Book&apos;</span>, backref=<span class="hljs-string">&apos;author&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;Author:%s&apos;</span> %self.name

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span><span class="hljs-params">(db.Model)</span>:</span>
    __tablename__ = <span class="hljs-string">&apos;books&apos;</span>
    id = db.Column(db.Integer, primary_key=<span class="hljs-keyword">True</span>)
    name = db.Column(db.String(<span class="hljs-number">64</span>))
    author_id = db.Column(db.Integer, db.ForeignKey(<span class="hljs-string">&apos;authors.id&apos;</span>))
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;Book:%s,%s&apos;</span>%(self.name,self.author_id)

<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello_world</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;Hello World!&apos;</span>


<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:

    db.drop_all()
    db.create_all()

    <span class="hljs-comment"># &#x751F;&#x6210;&#x6570;&#x636E;</span>
    au1 = Author(name=<span class="hljs-string">&apos;&#x8001;&#x738B;&apos;</span>)
    au2 = Author(name=<span class="hljs-string">&apos;&#x8001;&#x60E0;&apos;</span>)
    au3 = Author(name=<span class="hljs-string">&apos;&#x8001;&#x5218;&apos;</span>)
    <span class="hljs-comment"># &#x628A;&#x6570;&#x636E;&#x63D0;&#x4EA4;&#x7ED9;&#x7528;&#x6237;&#x4F1A;&#x8BDD;</span>
    db.session.add_all([au1, au2, au3])
    <span class="hljs-comment"># &#x63D0;&#x4EA4;&#x4F1A;&#x8BDD;</span>
    db.session.commit()
    bk1 = Book(name=<span class="hljs-string">&apos;&#x8001;&#x738B;&#x56DE;&#x5FC6;&#x5F55;&apos;</span>, author_id=au1.id)
    bk2 = Book(name=<span class="hljs-string">&apos;&#x6211;&#x8BFB;&#x4E66;&#x5C11;&#xFF0C;&#x4F60;&#x522B;&#x9A97;&#x6211;&apos;</span>, author_id=au1.id)
    bk3 = Book(name=<span class="hljs-string">&apos;&#x5982;&#x4F55;&#x624D;&#x80FD;&#x8BA9;&#x81EA;&#x5DF1;&#x66F4;&#x9A9A;&apos;</span>, author_id=au2.id)
    bk4 = Book(name=<span class="hljs-string">&apos;&#x600E;&#x6837;&#x5F81;&#x670D;&#x7F8E;&#x4E3D;&#x5C11;&#x5973;&apos;</span>, author_id=au3.id)
    bk5 = Book(name=<span class="hljs-string">&apos;&#x5982;&#x4F55;&#x5F81;&#x670D;&#x82F1;&#x4FCA;&#x5C11;&#x7537;&apos;</span>, author_id=au3.id)
    <span class="hljs-comment"># &#x628A;&#x6570;&#x636E;&#x63D0;&#x4EA4;&#x7ED9;&#x7528;&#x6237;&#x4F1A;&#x8BDD;</span>
    db.session.add_all([bk1, bk2, bk3, bk4, bk5])
    <span class="hljs-comment"># &#x63D0;&#x4EA4;&#x4F1A;&#x8BDD;</span>
    db.session.commit()

    app.run(debug=<span class="hljs-keyword">True</span>)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../chapter3/zong-he-an-4f8b-tu-shu-guan-li.html" class="navigation navigation-prev " aria-label="Previous page: 综合案例-图书管理"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html" class="navigation navigation-next " aria-label="Next page: 数据显示&amp;表单添加"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book 6节课入门Flask框架web开发 -->
</html>
